<?php
/*
Auth:jian
Desc:重写分页类 show方法
Date:2015-07-31
*/
namespace App\Lib;
class Page extends \Lib\Page{
	private $page_offset;
	public $offset;
	public $size;
	public $page_no;
	private $params = [];
	public function __construct($count,$page_size,$page_offset=3){
		$this->page_offset = $page_offset;
		$this->count = $count;
		$this->size = $page_size;
		$this->page_total = ceil($count/$page_size);
		$this->page_total =  $this->page_total;
		$this->page_no=request('page_no',1,'absint');
		if($this->page_no<=0){
			$this->page_no=1;
		}
		$this->offset = ($this->page_no-1)*$page_size;
		if(isset($_SERVER['QUERY_STRING'])){
			parse_str($_SERVER['QUERY_STRING'],$params);
			if(!empty($params)){
				if(isset($params['page_no']))
					unset($params['page_no']);
				$params = array_map(function($v){return htmlspecialchars($v);},$params);
			}
			$this->params = $params;
		}
	}
	  /**
		* 显示翻页按钮
		* @param bool $show_count 是否显示：共多少页的提示信息
	    * @param string $appendOtherQueryStr 自定义追加的url的get参数
		* @return string
		*/
	  public function show($show_count = true,$appendOtherQueryStr='') {
	            if($this->page_total<=1){
	                return;
	            }
	              $display_before = $display_after = "";
	            $params=$this->array3queryStr($this->params);
	            $params && $params .= '&amp;';
	            $url = htmlspecialchars(isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : $_SERVER['PATH_INFO']);
	            $offset = 8;//$this->page_offset;
	              $total_page=$this->page_total;
	            $page_no =$this->page_no>($total_page)?($total_page):$this->page_no;
	            if ($page_no <= 1) {
	                $display_before = " class='disabled'";
	            }
	            $size = $this->size;
	            $form = 1;
	            $to = $size;
	             if ($page_no > 1) {
	                 $form = (($page_no-1) * $size) +1;
	                 $to = $form + ($size-1);
	             }
	            $navibar ='';
	            if($show_count) $navibar = "<div class=\"pull-left\">总计 {$this->count} 条,当前显示第 {$form} 到 {$to} 条</div>\n";
	            $navibar .= "<div class=\"pull-right\">\n<ul class='pagination'>\n";
	            $navibar .= "<li{$display_before}><a href=\"$url?{$params}page_no=1{$appendOtherQueryStr}\" title=\"首页\"><i class=\"ace-icon fa fa-step-backward\"></i></a></li>\n";
	            $navibar .= "<li{$display_before}><a href=\"$url?{$params}&page_no=".($page_no-1)."{$appendOtherQueryStr} \"  title=\"上一页\"><i class=\"ace-icon fa fa-backward\"></i></a></li>\n";
	            /****  前2条...当前页...后2条 *****/
	            $start_page = $page_no -$offset;
	            $end_page =$page_no+$offset;
	            if($start_page<1){
	                $start_page=1;
	            }
	            if($end_page>$total_page) {
	                $end_page=$total_page;
	            }
	            for($i=$start_page;$i<=$end_page;$i++){
	                if($i==$page_no){
	                    $navibar.= "<li class=\"active\"><a href=\" $url?{$params}page_no=$i{$appendOtherQueryStr}\">$i</a></li>";
	                }else{
	                    $navibar.= "<li><a href=\" $url?{$params}page_no=$i{$appendOtherQueryStr} \">$i</a></li>";
	                }
	            }
	            if ($page_no >= $total_page) {
	                $display_after = " class='disabled'";
	            }
	            $navibar .= "<li{$display_after}><a href=\"$url?{$params}page_no=".($page_no+1)."{$appendOtherQueryStr}\"  title=\"下一页\"><i class=\"ace-icon fa fa-forward\"></i></a></li>\n ";
	            $navibar .= "<li{$display_after}><a href=\"$url?{$params}page_no=".($total_page)."{$appendOtherQueryStr}\"  title=\"末页\"><i class=\"ace-icon fa fa-forward\"></i></a></li>\n ";
	            $navibar .="<form role=\"form\" action=\"\" method=\"get\"><div class=\"input-group page-number pull-right\">
	                                    <input type=\"text\" name=\"page_no\" class=\"form-control\" placeholder=\"页数\">
	                                    <span class=\"input-group-btn\"><button type=\"submit\" class=\"btn btn-info btn-sm\">转到</button></span></div></form>";
	            $navibar.="</ul></div>";
	            return $navibar; 
	        }
//前端调用这个分页，可以居中，去掉了跳转的表单   md:fei 9-17
public function showStyleForFront($show_count = true) {
	            if($this->page_total<=1){
	                return;
	            }
	              $display_before = $display_after = "";
	            $params=$this->array3queryStr($this->params);
	            $params && $params .= '&amp;';
	            $url = htmlspecialchars(isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : $_SERVER['PATH_INFO']);
	            $offset = 2;//$this->page_offset;
	              $total_page=$this->page_total;
	            $page_no =$this->page_no>($total_page)?($total_page):$this->page_no;
	            if ($page_no <= 1) {
	                $display_before = " class='disabled'";
	            }
	            $size = $this->size;
	            $form = 1;
	            $to = $size;
	             if ($page_no > 1) {
	                 $form = (($page_no-1) * $size) +1;
	                 $to = $form + ($size-1);
	             }
				$navibar ='';
	           if($show_count) $navibar = "<div class=\"center\">总计 {$this->count} 条,当前显示第 {$form} 到 {$to} 条</div>\n";
	            $navibar .= "<div class=\"center\">\n<ul class='pagination'>\n";
	            $navibar .= "<li{$display_before}><a href=\"$url?{$params}page_no=1\" title=\"首页\"><i class=\"ace-icon fa fa-step-backward\"></i></a></li>\n";
	            $navibar .= "<li{$display_before}><a href=\"$url?{$params}&page_no=".($page_no-1)." \"  title=\"上一页\"><i class=\"ace-icon fa fa-backward\"></i></a></li>\n";
	            /****  前2条...当前页...后2条 *****/
	            $start_page = $page_no -$offset;
	            $end_page =$page_no+$offset;
	            if($start_page<1){
	                $start_page=1;
	            }
	            if($end_page>$total_page) {
	                $end_page=$total_page;
	            }
	            for($i=$start_page;$i<=$end_page;$i++){
	                if($i==$page_no){
	                    $navibar.= "<li class=\"active\"><a href=\" $url?{$params}page_no=$i \">$i</a></li>";
	                }else{
	                    $navibar.= "<li><a href=\" $url?{$params}page_no=$i \">$i</a></li>";
	                }
	            }
	            if ($page_no >= $total_page) {
	                $display_after = " class='disabled'";
	            }
	            $navibar .= "<li{$display_after}><a href=\"$url?{$params}page_no=".($page_no+1)."\"  title=\"下一页\"><i class=\"ace-icon fa fa-forward\"></i></a></li>\n ";
	            $navibar .= "<li{$display_after}><a href=\"$url?{$params}page_no=".($total_page)."\"  title=\"末页\"><i class=\"ace-icon fa fa-forward\"></i></a></li>\n ";
	            $navibar .="</ul>";
	             /* $navibar .="<form role=\"form\" action=\"\" method=\"post\"><div class=\"input-group page-number pull-right\">
	                                    <input type=\"text\" name=\"page_no\" class=\"form-control\" placeholder=\"页数\">
	                                    <span class=\"input-group-btn\"><button type=\"submit\" class=\"btn btn-info btn-sm\">转到</button></span></div></form>";*/
	            $navibar.="</div>";
	            return $navibar; 
	        }
	private function array3queryStr($array){
		if(empty($array)){
			return '';
		}
		$str = '';
		foreach($array as $key=>$val){
			$str .= "{$key}=".urlencode($val)."&amp;";
		}
		return substr($str,0,-5);
	}
}
?>